package cn.alcatraz.pet.controller;

import cn.alcatraz.basic.exception.BusinessException;
import cn.alcatraz.basic.utils.LoginContext;
import cn.alcatraz.org.domain.Employee;
import cn.alcatraz.org.service.IEmployeeService;
import cn.alcatraz.pet.dto.HandlerMsgDto;
import cn.alcatraz.pet.service.ISearchMasterMsgService;
import cn.alcatraz.pet.domain.SearchMasterMsg;
import cn.alcatraz.pet.query.SearchMasterMsgQuery;
import cn.alcatraz.basic.utils.PageList;
import cn.alcatraz.user.domain.Logininfo;
import cn.alcatraz.user.domain.User;
import cn.alcatraz.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.alcatraz.basic.utils.AjaxResult;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private IUserService userService;
    
    @Autowired private IEmployeeService employeeService;


    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpt(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return AjaxResult.myself();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delete(id);
            return AjaxResult.myself();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg loadById(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.loadById(id);
    }


    /**
    * 查看所有的信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> loadAll(){

        return searchMasterMsgService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> queryData(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryData(query);
    }
    /**
    * @Title: publish
    * @Description: 发布寻主
    * @Author: Alcatraz
    * @Version: 1.0
    * @Date:  2022/7/20 15:08
    * @Parameters: [searchMasterMsg, req]
    * @Return cn.alcatraz.basic.utils.AjaxResult
    */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest req){
        try {
            //给寻主消息的userId设置值
            Logininfo logininfo = LoginContext.getLogininfo(req);
            User user = userService.loadByLogininfoId(logininfo.getId());
            searchMasterMsg.setUserId(user.getId());
            //调用业务方法进行寻主消息发布
            searchMasterMsgService.publish(searchMasterMsg);
            return AjaxResult.myself();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    //前台用户查询寻主列表
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query, HttpServletRequest req){
        // 1.查询出user用户
        Logininfo logininfo = LoginContext.getLogininfo(req);
        User user = userService.loadByLogininfoId(logininfo.getId());
        query.setUserId(user.getId());
        return searchMasterMsgService.queryData(query);
    }


    //后台平台管理员查询待审核
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query, HttpServletRequest req){
        //查询user用户
        query.setState(0);
        return searchMasterMsgService.queryData(query);
    }


    //店铺任意员工都可以查看待处理
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest req){
        query.setState(1);
        //设置当前查询用户对应的店铺Id
        Logininfo logininfo = LoginContext.getLogininfo(req);
        Employee employee = employeeService.loadByLogininfoId(logininfo.getId());
        query.setShopId(employee.getShopId());
        return searchMasterMsgService.queryData(query);
    }


    //平台管理员和店铺店长都可以看到寻主池的消息
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery query, HttpServletRequest req){
        query.setState(1);
        return searchMasterMsgService.queryData(query);
    }

    //平台管理员和店铺店长都可以已完成的数据

    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query, HttpServletRequest req){
        //设置查询状态为审核通过
        query.setState(2);
        return searchMasterMsgService.queryData(query);
    }



    /**
     * @Title: accept
     * @Description: 店铺员工接单
     * @Author: Alcatraz
     * @Version: 1.0
     * @Date:  2022/7/23 21:28
     * @Parameters: [handlerMsgDto, req]
     * @Return cn.itsource.basic.utils.AjaxResult
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody HandlerMsgDto handlerMsgDto){
        try {
            searchMasterMsgService.accept(handlerMsgDto);
            return AjaxResult.myself();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    //拒单接口
    @PostMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId){
        try {
            searchMasterMsgService.reject(msgId);
            return AjaxResult.myself();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
}
